﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SqlServerDriver
{
	public partial class QuickDiffDialog : Form
	{
		public QuickDiffDialog(DbViewDriver.IConnectionConfig connectionConfig, String[]objectTypes, String objectTypeIn)
		{
			InitializeComponent();
			lblHelp.Text = HELP;
			this.connectionConfig = connectionConfig;
			this.holdConnectionInfo = connectionConfig.GetCurrentConnection();
			this.sourceConnectionInfo = this.holdConnectionInfo;
			// seed the database 
			this.sourceDb = this.sourceConnectionInfo.Name;
			this.targetDb = ""; // this.m_SourceConnection.Name;
			// 
			for (int idx = 0; idx < objectTypes.GetLength(0); ++idx)
			{
				String objectType = objectTypes[idx];
				this.comboCompare.Items.Add(objectType);
				if (Normalise(objectType) == Normalise(objectTypeIn))
					this.comboCompare.SelectedIndex = idx;
			}
			if (this.comboCompare.SelectedIndex == -1)
				this.comboCompare.SelectedIndex = 0;
		}
		
		String Normalise(String s)
		{
			return s.Replace(" ", "").ToUpper();
		}

		// Properties
		public String SourceDb
		{
			get { return this.sourceDb; }
			set {sourceDb = value; }
		}

		public String TargetDb
		{
			get { return this.targetDb; }
			set { SetDatabase2Name(value); }
		}
		
		public String ObjectType
		{
			get { return this.objectType; }
			set {
					this.objectType = value;
					comboCompare.SelectedItem = this.objectType;
				}
		}
		
		private void QuickDiff_Load(object sender, EventArgs e)
		{
			this.labelSourceDb.Text = this.sourceDb;
			if (this.targetDb.Length > 0)
				this.labelTargetDb.Text = this.targetDb;
			else
				this.labelTargetDb.Text = "Press [...] to select a comparison database";
			// ask for the connection directly 
			// no. It's too confusing. 
			// if ((this.m_sourceDb == this.m_targetDb))
				// DoBrowse();
		}
		
		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void BtnGo_Click(object sender, EventArgs e)
		{
			if (this.TargetDb == this.SourceDb)
			{
				MessageBox.Show("You are attempting to compare a database with itself.\n"+
								"There would nothing to report.\n"+
								"Press the [...] button to select another database",
								"Warning",MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else if (this.TargetDb.Length == 0)
			{
				MessageBox.Show("No comparison database selected.\n"+
								"Press the [...] button to select another database",
								"Warning",MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else
			{
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}

		private void ComboCompare_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboBox cb = sender as ComboBox;
			if (cb.SelectedIndex != -1)
				this.objectType = cb.Items[cb.SelectedIndex].ToString();
		}
		
		private void BtnBrowse_Click(object sender, EventArgs e)
		{
			DoBrowse();
		}

		private void DoBrowse()
		{
			String currentConn = this.targetDb;
			// connect
			// needs 
			this.connectionConfig.SetCurrentConnection(this.sourceConnectionInfo);
			SqlServerConnectionEditForm openDlg = new SqlServerConnectionEditForm(this.connectionConfig);
			if (openDlg.ShowDialog(this) == DialogResult.OK)
			{
				this.sourceConnectionInfo = this.connectionConfig.GetCurrentConnection();
				currentConn = this.sourceConnectionInfo.Name;
			}
			this.connectionConfig.SetCurrentConnection(this.holdConnectionInfo);
			SetDatabase2Name(currentConn);
		}
		
		void SetDatabase2Name(String connectionName)
		{
			this.targetDb = connectionName;
			this.labelTargetDb.Text = this.targetDb;
			this.labelTargetDb.ForeColor = System.Drawing.SystemColors.WindowText;
		}
		
		// members
		private String sourceDb = "";
		private String targetDb = "";
		private String objectType = "";
		
		private DbViewDriver.IConnectionConfig connectionConfig;
		private DbView.ConnectionInfo holdConnectionInfo;
		private DbView.ConnectionInfo sourceConnectionInfo;

		const String HELP = @"Compare objects in two databases. 

Select a target database and object type to compare with the currently connected database. The report will show the items in this database but not in the other in green and items present in the second database but not the this one  in red. When objects exist in both databases they are shown if the comparison detects a difference. 

Note not all attributes of the objects are compared. Use a speclised tool for a more precise check.
";
	}
}
